package com.jh3502.vivo.function.author.add;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.author.AuthorListBean;
import com.jh3502.vivo.function.author.AuthorPresenter;
import com.jh3502.vivo.function.author.chosen.AuthorChosenActivity;
import com.jh3502.vivo.function.contact.ContactActivity;
import com.jh3502.vivo.function.contact.ContactBean;
import com.jh3502.vivo.function.dialog.DialogListBig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/21 10:31
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class AuthorNewActivity extends BaseActivity<AuthorPresenter.AuthorView, AuthorPresenter> implements AuthorPresenter.AuthorView {
    public static final int FROM_PERSON_CHOOSE = 0;
    public static final int FROM_MSG_CHOOSE = 1;
    public static final int FROM_TIME_CHOOSE = 2;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.msg)
    TextView msg;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.used)
    CheckBox used;
    List<Map<String, Object>> permissions = new ArrayList<>(2);
    private String personName;
   
    private int endtime = 0;
    private int status = 0;
    private int userId;
    private String[] timeString;
    private int[] timeInt;

    @Override
    public AuthorPresenter initPresenter() {
        return new AuthorPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_author_new;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("委托授权");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mPresenter.setAuthorView(this);
        used.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                status = 1;
            } else {
                status = 0;
            }
        });
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_done, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_done:
                Map<String, Object> body = new HashMap<>(2);
                body.put("userId", userId);
                body.put("name", personName);
                body.put("permissions", permissions);
                body.put("endtime", endtime);
                body.put("status", status);
                mPresenter.toSubmitEntrust(body);
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {

        timeString = getResources().getStringArray(R.array.timeString);
        timeInt = getResources().getIntArray(R.array.timeInt);
        
        mPresenter.toFindMyEntrust();

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);

    }

    @OnClick(R.id.name)
    public void onNameClicked() {
        Intent intent = new Intent(getActContext(), ContactActivity.class);
        intent.putExtra("singlechosen", true);
        intent.putExtra("weituo",true);
        startActivityForResult(intent, FROM_PERSON_CHOOSE);
    }

    @OnClick(R.id.msg)
    public void onMsgClicked() {
        Intent intent = new Intent(getActContext(), AuthorChosenActivity.class);
        startActivityForResult(intent, FROM_MSG_CHOOSE);
    }

    @OnClick(R.id.time)
    public void onTimeClicked() {
        Intent intent = new Intent(getActContext(), DialogListBig.class);
        intent.putExtra("list", timeString);
        intent.putExtra("title", "授权期限");
        startActivityForResult(intent, FROM_TIME_CHOOSE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case FROM_PERSON_CHOOSE:
                    List<ContactBean> listC = new Gson().fromJson(data.getStringExtra("datachosen"),
                            new TypeToken<List<ContactBean>>() {
                            }.getType());

                    personName = listC.get(0).getName();
                    userId = listC.get(0).getUserId();
                    name.setText(personName);
                    break;
                case FROM_MSG_CHOOSE:
                    permissions.clear();
                    List<AuthorBean> listA = new Gson().fromJson(data.getStringExtra("datachosen"),
                            new TypeToken<List<AuthorBean>>() {
                            }.getType());
                    StringBuffer stringBuffer = new StringBuffer();

                    for (AuthorBean authorBean : listA) {
                        stringBuffer.append(authorBean.getTypename());
                        stringBuffer.append(authorBean.getName());
                        stringBuffer.append(" ");

                        Map<String, Object> map = new HashMap<>(2);
                        map.put("smallType", authorBean.getSmallType());
                        map.put("name", authorBean.getTypename() + authorBean.getName());
                        permissions.add(map);
                    }
                    msg.setText(stringBuffer.toString());
                    break;
                case FROM_TIME_CHOOSE:
                    
                    int chosen = data.getIntExtra("chosen", -1);
                    time.setText(timeString[chosen]);
                    endtime = timeInt[chosen];
                    break;
                default:
            }
        }
    }

    @Override
    public void findPermissListSuccess(String s) {

    }

    @Override
    public void submitEntrustSuccess() {
        showShortToast("新建授权成功");
        onBackPressed();

    }

    @Override
    public void findMyEntrustSuccess(List<AuthorListBean> dataList) {

    }

    @Override
    public void delEntrustSuccess() {

    }

    @Override
    public void enableEntrustSuccess() {

    }
}
